<template>
    <div class="createPost-container">
        <el-form
        :model="basicForm"
        :rules="rules"
        ref="basicForm"
        label-width="100px"
        class="demo-basicForm"
        >

            <el-form-item label="网站标题" label-width="120px" prop="title">
                <el-input v-model="basicForm.title"></el-input>
            </el-form-item>

            <el-form-item label="关键词" label-width="120px" prop="keywords">
                <el-input v-model="basicForm.keywords"></el-input>
            </el-form-item>

            <el-form-item label="网站描述" label-width="120px" prop="description">
                <el-input type="textarea" :rows="3" v-model="basicForm.description"></el-input>
            </el-form-item>

            <el-form-item label="公司LOGO" label-width="120px" prop="logo">
                <Upload v-model="basicForm.logo" />
            </el-form-item>

            <el-form-item label="手机号" label-width="120px" prop="telphone">
                <el-input v-model="basicForm.telphone"></el-input>
            </el-form-item>

            <el-form-item label="营销QQ账号" label-width="120px" prop="qq">
                <el-input v-model="basicForm.qq"></el-input>
            </el-form-item>

            <el-form-item label="QQ展示图片" label-width="120px" prop="qqImg">
                <Upload v-model="basicForm.qqImg" />
            </el-form-item>

            <el-form-item label="新浪微博账号" label-width="120px" prop="sina">
                <el-input v-model="basicForm.sina"></el-input>
            </el-form-item>

            <el-form-item label="微信账号" label-width="120px" prop="wxNo">
                <el-input v-model="basicForm.wxNo"></el-input>
            </el-form-item>

            <el-form-item label="微信展示图片" label-width="120px" prop="wxImg">
                <Upload v-model="basicForm.wxImg" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('basicForm')" :loading="loading">立即更新</el-button>
                <el-button @click="resetForm('basicForm')">重置</el-button>
            </el-form-item>

        </el-form>
  </div>
</template>

<script>

import {contactList, contactUpdate, contactCreate} from '@/api/basic.js'
import Upload from "@/components/Upload/SingleImage";

export default {
    name: 'Basic',
    components: { Upload },
    data (){
        return {
            loading: false,
            basicForm: {
                id: 0,
                title: '',
                keywords: '',
                description: '',
                logo: '',
                qq: '',
                qqImg: '',
                sina: '',
                telphone: '',
                wxImg: '',
                wxNo: ''
            },
            rules: {
                title: [{required: true, message: '请填写网站标题', trigger: 'blur'}],
                keywords: [{required: true, message: '请填写关键词', trigger: 'blur'}],
                description: [{required: true, message: '请填写网站描述', trigger: 'blur'}],
                logo: [{required: true, message: '请上传公司LOGO', trigger: 'blur'}],
                telphone: [{required: true, message: '请填写营销手机号', trigger: 'blur'}],
                qq: [{required: true, message: '请填写营销QQ账号', trigger: 'blur'}],
                qqImg: [{required: true, message: '请上传营销QQ图片', trigger: 'blur'}],
                sina: [{required: true, message: '请填写新浪微博账号', trigger: 'blur'}],
                wxImg: [{required: true, message: '请上传微信展示图片', trigger: 'blur'}],
                wxNo: [{required: true, message: '请填写微信账号', trigger: 'blur'}]
            }
        }
    },
    created(){
        this.getBasicInfo();
    },
    methods: {
        // 获取基础配置信息
        getBasicInfo(){
            contactList({pageSize: 1, pageType: 'A'}).then(response => {
                if (response.count > 0)
                {
                    let item = response.data[0];

                    this.basicForm.id = item.id;
                    this.basicForm.title = item.title;
                    this.basicForm.keywords = item.keywords;
                    this.basicForm.description = item.description;
                    this.basicForm.logo = item.logo;
                    this.basicForm.telphone = item.telphone;
                    this.basicForm.qq = item.qq;
                    this.basicForm.qqImg = item.qqImg;
                    this.basicForm.sina = item.sina;
                    this.basicForm.wxImg = item.wxImg;
                    this.basicForm.wxNo = item.wxNo;
                }
            });

            console.log(this.basicId);
        },
        // 提交
        submitForm(formName) {
            console.log(formName);
            this.$refs[formName].validate(async valid => {
                if (valid) {
                    this.loading = true;

                    if (this.basicForm.id > 0)
                    {
                        // 编辑
                        contactUpdate(this.basicForm).then(response => {
                            this.loading = false;
                            if (response.success == true)
                            {
                                this.$notify({
                                    title: "成功",
                                    message: "网站信息更新成功",
                                    type: "success",
                                    duration: 2000
                                });
                            }
                            else
                            {
                                this.$notify({
                                    title: "失败",
                                    message: "网站信息更新失败",
                                    type: "error",
                                    duration: 2000
                                });
                            }
                        });
                    }
                    else
                    {
                        // 新增
                        contactCreate(this.basicForm).then(response => {
                            this.loading = false;
                            if (response.success == true)
                            {
                                this.$notify({
                                    title: "成功",
                                    message: "网站信息更新成功",
                                    type: "success",
                                    duration: 2000
                                });
                            }
                            else
                            {
                                this.$notify({
                                    title: "失败",
                                    message: "网站信息更新失败",
                                    type: "error",
                                    duration: 2000
                                });
                            }
                        });
                    }

                }
                else
                {
                    console.log('submit error');
                    return false;
                }
            });
        },
        // 重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}

</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;
  margin-top: 20px;
  padding: 10px;
  box-sizing: border-box;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
